package morncloud.com.notes.ui;

import android.app.Fragment;
import android.content.Intent;
import android.util.Log;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.GridView;

import java.util.ArrayList;

import morncloud.com.notes.EditNoteActivity;
import morncloud.com.notes.MainActivity;
import morncloud.com.notes.R;
import morncloud.com.notes.database.DAO;


/**
 * Created by majoy on 2015/2/12.
 *
 */
public abstract class AbsListViewFragment extends Fragment {
    private ArrayList<Long> mDeleteArray = new ArrayList<Long>();
    private DataChangeCallbacks mDataChangeCallbacks;

    /**
     * @param AbsListView will bind cursor adapter, when onCreate ,it can be realized
     */
    public abstract void bindCursor(AbsListView AbsListView);

    /**
     * @param adapter     when the data set has been changed ,the view will be changed
     * @param absListView the ablistView such as GridView ,ListView and so on
     */
    public void bindMutilListener(final CursorAdapter adapter, final AbsListView absListView) {
        absListView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
        absListView.setMultiChoiceModeListener(new GridView.MultiChoiceModeListener() {

            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                // judge the item is checked or not ,and the item is in the delete array
                if (mDeleteArray != null) {
                    if (absListView.isItemChecked(position)) {
                        if (!mDeleteArray.contains(((Long) id))) {
                            mDeleteArray.add(id);
                        }
                    } else {
                        if (mDeleteArray.contains(((Long) id))) {
                            mDeleteArray.remove(((Long) id));
                        }
                    }
                }
                adapter.notifyDataSetChanged();
                mode.invalidate();
            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                getActivity().getMenuInflater().inflate(R.menu.item_delete, menu);
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return true;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                // case if id == delete
                if (item.getItemId() == R.id.delete) {
                    //TODO do delete
                    Log.e("print", "delete is pressed");
                    // then we judge the array, and delete the id
                    DAO.getInstance(getActivity()).deleteNotesWithId(mDeleteArray);
                    mDeleteArray.clear();
                    if (mDataChangeCallbacks!=null){
                        mDataChangeCallbacks.reloadView();
                    }
                }
                mode.invalidate();
                adapter.notifyDataSetChanged();
                return true;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                // to clear the array
                if (mDeleteArray.size() > 0 && mDeleteArray != null) {
                    mDeleteArray.clear();
                }
            }
        });

    }

    /**
     * bind the AbListView onItemClickListener
     *
     * @param absListView
     */
    public void bindItemClickListener(AbsListView absListView) {
        absListView.setOnItemClickListener(new GridViewOnItemClickListener());
    }

    /**
     * bind the data set has been changed
     * @param changeCallbacks
     */

    public void bindDataChangeCallbacks(DataChangeCallbacks changeCallbacks) {
        this.mDataChangeCallbacks = changeCallbacks;
    }

    private class GridViewOnItemClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent();
            intent.setClass(getActivity(), EditNoteActivity.class);
            intent.putExtra(Utils.NOTES_STATUS_KEY, Utils.NOTES_UPDATE);
            intent.putExtra(Utils.NOTES_UPDATE_ID, id);
            getActivity().startActivityForResult(intent, MainActivity.CODE_RELOAD_VIEW);
        }
    }
}
